package com.bood.shimmer.config;

import com.bood.shimmer.common.enums.BizExceptionEnum;
import com.bood.shimmer.common.enums.ResultEnum;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.ResponseMessageBuilder;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.ResponseMessage;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * Swagger2 配置
 *
 * <a>http://127.0.0.1:8080/shimmer-fast/doc.html</a>
 * </p>
 *
 * @author：bood
 * @date：2020/9/25
 */
@Profile({"dev", "test"})
@EnableSwagger2
@Configuration
public class Swagger2Config implements WebMvcConfigurer {

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .globalResponseMessage(RequestMethod.GET, getResponseMessage())
                .globalResponseMessage(RequestMethod.POST, getResponseMessage())
                .globalResponseMessage(RequestMethod.PUT, getResponseMessage())
                .globalResponseMessage(RequestMethod.DELETE, getResponseMessage())
                .globalResponseMessage(RequestMethod.OPTIONS, getResponseMessage())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.bood.shimmer.modules.*.controller"))
                .paths(PathSelectors.any())
                .build()
                .apiInfo(apiInfo());
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("shimmer-fast API")
                .description("shimmer-fast 是一款基于 SpringBoot + MyBatis-Plus 的[轻量级]前后端分离[快速]开发脚手架，拥有轻量的权限认证功能，开箱即用！")
                .contact(new Contact("bood", "https://blog.csdn.net/BUG_call110", "mr_tsui@foxmail.com"))
                .termsOfServiceUrl("https://blog.csdn.net/BUG_call110")
                .version("1.0")
                .build();
    }


    private List<ResponseMessage> getResponseMessage() {
        // 全局状态响应码
        List<ResponseMessage> responseMessageList = new ArrayList<>();

        // 通用结果集响应码
        responseMessageList.add(
                new ResponseMessageBuilder().code(ResultEnum.DEAL_SUCCESS.code()).message(ResultEnum.DEAL_SUCCESS.message()).build());
        responseMessageList.add(
                new ResponseMessageBuilder().code(ResultEnum.BODY_NOT_MATCH.code()).message(ResultEnum.BODY_NOT_MATCH.message()).build());
        responseMessageList.add(
                new ResponseMessageBuilder().code(ResultEnum.SIGNATURE_NOT_MATCH.code()).message(ResultEnum.SIGNATURE_NOT_MATCH.message()).build());
        responseMessageList.add(
                new ResponseMessageBuilder().code(ResultEnum.NOT_FOUND.code()).message(ResultEnum.NOT_FOUND.message()).build());
        responseMessageList.add(
                new ResponseMessageBuilder().code(ResultEnum.INTERNAL_SERVER_ERROR.code()).message(ResultEnum.INTERNAL_SERVER_ERROR.message()).build());

        // 业务异常响应码
        responseMessageList.add(
                new ResponseMessageBuilder().code(BizExceptionEnum.ILLEGAL_ARGUME.code()).message(BizExceptionEnum.ILLEGAL_ARGUME.message()).build());
        responseMessageList.add(
                new ResponseMessageBuilder().code(BizExceptionEnum.REPETITIVE_OPERATION.code()).message(BizExceptionEnum.REPETITIVE_OPERATION.message()).build());
        responseMessageList.add(
                new ResponseMessageBuilder().code(BizExceptionEnum.DUPLICATE_KEY.code()).message(BizExceptionEnum.DUPLICATE_KEY.message()).build());

        return responseMessageList;
    }

}
